import {defineStore} from 'pinia'
import {computed, ref} from 'vue'

export const useStore = defineStore('store', () => {
    const addFile = ref(false)
    const fileHtml = ref(null)
    const showFloatingBox = ref(false)
    const cookie = ref(localStorage.getItem('cookie'))
    const verified = computed(()=>{ return !!cookie.value })
    const newFileStatus = ref(false)
    const exportAsDocxStatus = ref(false)
    const fileName = ref('无标题')
    const currentDocId = ref(null)
    const openFileStatus = ref({
        status: false,
        id: '',
        fileHtml: '',
        fileName: ''
    })
    function setCookie(str) {
        str = String(str)
        cookie.value = str
        localStorage.setItem('cookie', str)
    }
    function generateUniqueId() {
        const timestamp = Date.now().toString()
        const randomNum = Math.floor(Math.random() * 10000).toString().padStart(4, '0')
        currentDocId.value =  timestamp + randomNum
    }
    generateUniqueId()
    return { addFile, fileHtml, showFloatingBox, verified, cookie, setCookie, newFileStatus, exportAsDocxStatus, fileName,
        currentDocId, generateUniqueId, openFileStatus }
})
